﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading;
using System.ComponentModel;

namespace ControlExplorer
{
    public partial class SamplePage 
        : PhoneApplicationPage
    {
        public SamplePage()
        {
            InitializeComponent();
        }


        public class ControlAndSampleViewModel
            : INotifyPropertyChanged
        {
            public ControlViewModel CurrentControl { get; set; }

            SampleViewModel _sample;
            public SampleViewModel CurrentSample 
            { 
                get 
                {
                    return _sample;
                }
                set 
                { 
                    _sample = value; 
                    if (PropertyChanged != null)
                        PropertyChanged (this, new PropertyChangedEventArgs("CurrentSample"));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            int controlIndex, sampleIndex;
            if (int.TryParse(NavigationContext.QueryString["control"], out controlIndex))
            {
                if (int.TryParse(NavigationContext.QueryString["sample"], out sampleIndex))
                {
                    var controlVM = ViewModel.Instance.Controls[controlIndex];
                    DataContext = new ControlAndSampleViewModel()
                    {
                        CurrentControl = controlVM,
                        CurrentSample = controlVM.Samples[sampleIndex]
                    };

                    OnInitializeSample();
                }
            }
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);

            DataContext = null;

            // Add by David for TFS-33368.
            ApplicationBar.Buttons.Clear();
            ApplicationBar.MenuItems.Clear();
            // End add.
        }


        private void OnInitializeSample()
        {
            var context = (ControlAndSampleViewModel)DataContext;

            ApplicationBar.Buttons.Clear();
            ApplicationBar.MenuItems.Clear();

            var exposeActions = context.CurrentSample.Demo as IExposeApplicationBarItems;
            if (exposeActions != null)
            {
                foreach (var item in exposeActions.ApplicationBarItems)
                {
                    if (item is IApplicationBarIconButton && ApplicationBar.Buttons.Count < 4)
                    {
                        ApplicationBar.Buttons.Add(item);
                    }
                    else if (item is IApplicationBarMenuItem)
                    {
                        ApplicationBar.MenuItems.Add(item);
                    }
                }
            }

            var exposeOrientation = context.CurrentSample.Demo as IExposePageOrientation;
            if (exposeOrientation != null) 
            {
                this.SupportedOrientations = exposeOrientation.Orientation;
            }

            if (ApplicationBar.Buttons.Count + ApplicationBar.MenuItems.Count > 0)
            {
                ApplicationBar.IsVisible = true;
            }
            else
            {
                ApplicationBar.IsVisible = false;
            }
        }
    }
}